<?php
//$Id: AreadataController.php 606 2012-12-16 05:19:12Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

Class GroupsController extends AdminController
{
    public $type = '';

    public function beforeAction() {
        $types = $this->_getGroupsModule()->getTypes();
        $this->setVar('types', $types);

        $type = strtolower(get_request('type'));
        if(!isset($types[$type])){
            $type = current(array_keys($types));
        }
        $this->type = $type;
        $this->setVar('type', $this->type);
    }

    public function ztreeAction() {
        $cartree = Base::getObject('service.CarClassModule')->getData();
        $this->setVar('cartree',  $cartree);
        $this->render('admin.config.groups_ztree');
    }

    public function tree1Action() {
        //$cartree = Base::getObject('service.CarClassModule')->getData()->showTree('classtree');
        //$this->setVar('cartree',  $cartree);

        $cartree = Base::getObject('service.CarClassModule')->getData();
        $this->setVar('cartree',  $cartree);
        $this->render('admin.config.groups_tree1');
    }

    public function tree2Action() {
        echo Base::getObject('service.CarClassModule')->getData()->getJSONList(get_request('root'));
    }

    public function indexAction() {
        $list = $this->_getGroupsModule()->getTypeList($this->type);
        $this->setVar('list',  $list);
        $this->render('admin.config.groups');
    }

    public function saveAjaxAction() {
        if(get_request('submitform')) {
            $this->_beforeSave();

            $names  = get_request('groupname');
            $images = get_request('groupimage');

            if($names){
                $stmt = Db::prepare("UPDATE ".Db::table('groups')." SET name = ?, image = ? WHERE gid = ?");
                $stmt->bind_param('ssi', $_name, $_image, $_gid);
                foreach($names as $_gid => $_name){
                    $_image = $images[$_gid];
                    $stmt->execute();
                }
                $stmt->close();
            }

            $names  = get_request('new_groupname');
            $images = get_request('new_groupimage');
            if($names){
                $stmt = Db::prepare("INSERT INTO ".Db::table('groups')." SET name = ?, image = ?, type = ?");
                $stmt->bind_param('sss', $_name, $_image, $_type);
                foreach($names as $_key => $_name){
                    $_image = $images[$_key];
                    $_type  = $this->type;
                    $stmt->execute();
                }
                $stmt->close();
            }

            $this->renderJSON(array('success'=>true, 'message'=>'数据更新成功'));
        }
    }

    public function delAjaxAction() {
        $gid = intval(get_request('gid', 0));
        $this->_getGroupsManager()->delete($gid);
        $result = array('success'=>true);
        $this->renderJSON($result);
    }

    private function _beforeSave(){
        //foreach(array('groupname', 'new_groupname') as $item){
            $arr = get_request('groupname', array()) + get_request('new_groupname', array());
            foreach($arr as $tmp){
                if(empty($tmp)){
                    $this->renderJSON(array('success'=>false, 'message'=>'用户组名称不能为空'));
                    break;
                }
            }
        //}
    }

    private function _getGroupsModule() {
        return Base::getObject('service.GroupsModule')->fetchDb();
    }

    private function _getGroupsManager() {
        return Base::getObject('service.GroupsManager');
    }
}

